﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Atlantyda
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void ExitMenuItem_Click(object sender, EventArgs e)
        {
            this.Disconect();
            Application.ExitThread();
            Application.Exit();
        }

        private void AboutMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox about = new AboutBox();
            about.Show(this);
        }

        private void ConnectMenuItem_Click(object sender, EventArgs e)
        {
            Agent.LoginForm = new LoginForm(this);
            Agent.LoginForm.Show(this);
        }

        private void AgentPanel_Paint(object sender, PaintEventArgs e)
        {
            AgentConsole.Width = this.Width - 40;
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Disconect();
        }

        private void DisconnectMenuItem_Click(object sender, EventArgs e)
        {
            this.Disconect();
            this.HideLoadingBox();
        }

        public void Disconect()
        {
            try
            {
                if (Agent.Connected)
                {
                    Agent.Disconnect();
                    DisconnectMenuItem.Visible = false;
                    ConnectMenuItem.Visible = true;
                    AgentPanel.Visible = false;
                    LoadingBox.Visible = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void MainForm_Paint(object sender, PaintEventArgs e)
        {
            this.RenderMapUpdate();
        }

        private void AgentConsole_KeyUp(object sender, KeyEventArgs e)
        {
            if (Agent.Connected)
            {
                switch (e.KeyCode)
                {
                    case Keys.Up: Agent.StepForward();
                        break;
                    case Keys.Left: Agent.RotateLeft();
                        break;
                    case Keys.Right: Agent.RotateRight();
                        break;
                    case Keys.Space: Agent.Recharge();
                        break;
                    case Keys.L: Agent.Look();
                        break;
                    case Keys.S: Agent.Speak("Tekst testowy");
                        break;
                    case Keys.Menu: Agent.Map.RenderMap();
                        break;
                }
                //Agent.AddConsoleInformation("Aktualna energia: " + Agent.ActualEnergy);
                //Agent.AddConsoleInformation("Zużyta energia przez agent: " + Agent.UsedEnergy);
                //Agent.AddConsoleInformation("Uzyskana energia przez agenta: " + Agent.ReceivedEnergy);
            }
        }

        private void MainForm_SizeChanged(object sender, EventArgs e)
        {
            this.RenderMapUpdate();
        }

        private void RenderMapUpdate()
        {
            if (Agent.Connected && Agent.Map.Size > 0)
            {
                Agent.Map.RenderMap();
            }
        }

        public void HideLoadingBox()
        {
            this.LoadingBox.InvokeEx(box => box.Visible = false);
        }

        public void ShowLoadingBox()
        {
            this.LoadingBox.InvokeEx(box => box.Visible = true);
        }

        private void AgentInfo_Paint(object sender, PaintEventArgs e)
        {
            this.AgentInfo.Height = this.AgentPanel.Height - this.AgentConsole.Height - 20;
        }
    }
}
